package club.cearnach.security.strategy;

import club.cearnach.core.property.MovieSecurityProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.web.session.SessionInformationExpiredEvent;
import org.springframework.security.web.session.SessionInformationExpiredStrategy;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 并发登录策略
 *
 * @author 阮胜
 * @date 2018/5/23 15:37
 */
@Component
@Slf4j
public class ExpiredSessionStrategy implements SessionInformationExpiredStrategy {
    @Autowired
    private MovieSecurityProperties movieSecurityProperties;
    @Override
    public void onExpiredSessionDetected(SessionInformationExpiredEvent event) throws IOException, ServletException {
        log.info("Expired session , redirectURI = " + event.getRequest().getRequestURI());
        HttpServletResponse resp = event.getResponse();
        resp.sendRedirect(movieSecurityProperties.getAdminLoginExpiredUrl());
    }
}
